package com.gdufs.secondhandwebsite.controller;

import com.gdufs.secondhandwebsite.entity.Goods;
import com.gdufs.secondhandwebsite.entity.GoodsType;
import com.gdufs.secondhandwebsite.service.GoodsService;
import com.gdufs.secondhandwebsite.service.ex.UserNotLoginException;
import com.gdufs.secondhandwebsite.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@Api(tags = "二手交易网站闲置商品类相关接口")
@RestController
@RequestMapping("goods")
public class goodsController extends BaseController{
    @Autowired
    GoodsService goodsService;

    @ApiOperation(value = "新建闲置商品接口", notes = "需传入商品基本信息")
    @PostMapping("/create")
    public JsonResult<Long> createGoods(@RequestBody Goods goods, HttpSession httpSession) {
//        获取当前发布商品的用户id
        String userName = getUsernameFromSession(httpSession);
        String userImg =getUserImgFromSession(httpSession);

        goodsService.insertGoods(goods,userName,userImg);
        System.out.println("新增闲置物品id："+goods.getGoodsId());
        return new JsonResult<Long>(OK,goods.getGoodsId());
    }

    @ApiOperation(value = "更新闲置商品信息接口", notes = "可选择性地更新闲置商品内容的任意字段")
    @PostMapping("/update")
    public JsonResult<Goods> updateGoods(@RequestBody Goods goods){
        boolean is_update =goodsService.updateGoods(goods);
        JsonResult jsonResult = new JsonResult<Goods>(OK);
        jsonResult.setData(goods);
        if(is_update){
            jsonResult.setMessage("数据更新成功");
        }else {
            jsonResult.setMessage("数据更新失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "获取闲置商品接口", notes = "根据闲置商品Id获取")
    @GetMapping("{id}/details")
    public JsonResult<Goods> getById(@PathVariable("id") Integer id) {
        // 调用业务对象执行获取数据
      Goods result = goodsService.getGoodsById(id.longValue());
        return new JsonResult<Goods>(OK, result);

    }

    @ApiOperation(value = "上传闲置商品图片接口")
    @PostMapping("/uploadimg")
    public JsonResult<String> uploadImg(@RequestParam("file") MultipartFile file) throws IOException{
        // 获取文件名
        String fileName = file.getOriginalFilename();

        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));


        // 解决中文问题，liunx下中文路径，图片显示问题
        fileName = UUID.randomUUID().toString().replace("-", "") + suffixName;

        File dest = new File("F:/upload/goods-pic",fileName);

        // File dest = new File(ApiConstant.DEV_UPLOAD_PATH + fileName);

        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest);
        return new JsonResult<String>(OK,fileName);
    }

    @ApiOperation(value = "下架闲置商品信息接口", notes = "根据闲置商品Id下架")
    @DeleteMapping("{id}/delete")
    public JsonResult<Void> deleteById(@PathVariable("id") Integer id) {
        // 调用业务对象执行获取数据
         boolean is_del = goodsService.removeGoods(id.longValue());
        JsonResult jsonResult = new JsonResult<Goods>(OK);
        if(is_del){
            jsonResult.setMessage("闲置物品下架成功");
        }else {
            jsonResult.setMessage("闲置物品下架失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "获取所有闲置商品接口")
    @GetMapping("all")
    public JsonResult<List<Goods>> getGoodsTypeGroup(){
        List<Goods> result =goodsService.getAllGoods();
        JsonResult jsonResult = new JsonResult<List<Goods>>(OK);
        jsonResult.setData(result);
        return  jsonResult;
    }

    @ApiOperation(value = "根据闲置商品类别获取商品接口")
    @PostMapping("typename")
    public JsonResult<List<Goods>> getByTypeName(String typename) {
        List<Goods> result =goodsService.SelectByGoodsType(typename);
        JsonResult jsonResult = new JsonResult<List<GoodsType>>(OK);
        jsonResult.setData(result);
        return  jsonResult;
    }

}
